package com.shop.action;

import java.util.List;

import com.shop.pojo.Book;
import com.shop.pojo.Booktype;

public class BookAction extends SysAction {
	
	private List<Book> books;
	private int log;
	
	private Integer pageNum = 1;// 当前页数
	private Integer pageCount;// 总页数
	private Integer rows = 10;// 行数
	private Book book = new Book();
	private List<Booktype> booktypes;
	private String idBook;
	private Integer num;

	

	

	public List<Booktype> getBooktypes()
	{
		return booktypes;
	}

	public void setBooktypes(List<Booktype> booktypes)
	{
		this.booktypes = booktypes;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public List<Book> getBooks() {
		return books;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public long getPageCount() {
		return pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public void setBooks(List<Book> books) {
		this.books = books;
	}

	public int getLog() {
		return log;
	}

	public void setLog(int log) {
		this.log = log;
	}

	public String getIdBook() {
		return idBook;
	}

	public void setIdBook(String idBook) {
		this.idBook = idBook;
	}

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	@SuppressWarnings("unchecked")
	public String findBooktype(){
	
		booktypes=getBookService().getTypes();
		for (Booktype booktype : booktypes)
			{
				System.err.println(booktype.getBooktypename());
			}
		return "booktypes";
	}
	public String findBooks() {
	//	System.out.println("aaaaaaaaaaaaaaaaaaaaa"+book.getBooktype().getIdBooktype());
		long rowTotal = getBookService().getTotal(book);
		long remained = rowTotal % rows;
		if (remained == 0) {
			pageCount = (int) (rowTotal / rows);

		} else {

			pageCount = (int) (rowTotal / rows + 1);
		}
		if (pageNum > pageCount) {
			pageNum = pageCount;
			log=1;//最后一页
		}
		if (pageNum < 1) {
			pageNum = 1;
			log=2;//第一页
		}

		books = getBookService().findBooks(book, pageNum, rows);
		if(books.size()<=0){
			log=3;//查不到符合条件的图书
		}
		return "allBooks";
	}
public String buy(){
	System.out.println(idBook);
	if(getBookService().buy(idBook)){
		return "buy";
	}
	return "index";
}
public String change(){
	if(getBookService().changeSize(idBook,num)){
		return "changed";
		
	}
	return "index";
}
}
